import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';

import {CityPickerService} from "../../../../provider/city-picker";

//导入三级跳转源页面
import {addTrenchPage} from '../addTrench'

//actionsheet需要导入的组件
import {Platform, ActionSheetController} from 'ionic-angular';
//接受从上个页面传递过来的数据
import {NavParams} from 'ionic-angular';
import {Http, Headers} from '@angular/http';
import {interfaceUrls} from '../../../../provider/serverUrls'
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/timeout';

//产品信息页面需要引入的模块
import {ModalController} from 'ionic-angular';

//商品信息页面
import {productPage} from './productPage/productPage';

@Component({
    selector: 'page-inexistDealer', templateUrl: 'inexistDealer.html'
})
export class inexistDealerPage {
    cityData: any[]; //城市数据
    cityName: string = '请选择您的城市'; //初始化城市名
    code: string; //城市编码
    areadata: any;
    data: any;
	product:any;

    companyName;     //公司名称
    textarea;       //详细地址信息
    remarks;        //对代理产品进行备注
	 trenchName;
    trenchMobile;
    
    companyAddressId;
    param = {};

    private headers = new Headers({'Content-Type': 'application/json;chartset=utf-8'});

    isShow: boolean;
    isNeed: boolean;

    constructor(
    	public navCtrl: NavController,
    	public cityPickerSev: CityPickerService,
    	public platform: Platform, 
    	public actionsheetCtrl: ActionSheetController,
    	private navParams: NavParams, 
    	public http: Http, 
    	public modalCtrl: ModalController
    	) 
	    {
	        this.setCityPickerData();
	        this.data = this.navParams.get('data');
	        console.info(this.data);
	        
	        this.product = this.navParams.get('product');
	        console.info("接受来自input框中的数据02",this.product);
	        //接收个人信息和电话
	        this.trenchName = this.navParams.get('trenchName');
	        this.trenchMobile = this.navParams.get('trenchMobile');
	        
	        
	
	    };

    setCityPickerData() {
        this.cityPickerSev.getCitiesData()
            .then(data => {
                this.cityData = data;
            });
    }

    /**
     * 城市选择器被改变时触发的事件
     * @param event
     */
    cityChange(event) {
        this.areadata = event;
        console.log(event, this.areadata);
        this.code = event['region'].value;
        console.info("??????本身事件触发的查看code+", this.code)
    }

    addBack() {
        this.navCtrl.pop();
    }

    addNext() {

    }

    //选择代理酒产品的菜单


    openModal(characterNum) {

        let modal = this.modalCtrl.create(productPage, {product: this.product});
        modal.present();
    }

    //当前页面取消按钮的方法
    cancel() {
        this.navCtrl.popToRoot();
    }

    //确定按钮的方法
    confirm() {
        this.isShow = true;
    }

    cancelAdd() {
        console.info("模态框消失")
        this.isShow = false;
    }

    //把新建的数据传给后台
    correctAdd() {


        //在这里进行后端传值             需要注意data.name       data.mobile 也需要传给后端
        if (!this.areadata) {
            this.isNeed = true;
        } else {
            this.isNeed = false;
            this.http
                .post(interfaceUrls.dealerAdd, JSON.stringify(this.param = {
                    id: this.areadata.city.value + this.areadata.province.value + this.areadata.region.value, //这里讲城市的ID值传过去是用一串还是怎么样
                    "companyName": this.companyName,
                    "mobile": this.data.mobile,
                    "name": this.data.name,
                    "companyAddressId": this.companyAddressId,
                    "remarks": this.remarks
                }), {headers: this.headers})
                .timeout(4000)
                .toPromise()
                .then((response) => {
                    console.info(response);
                    this.navCtrl.popToRoot();       //确定之后将数据添加到经销商页面
                })
                .catch((err) => {
                    console.info(err)
                })

            console.info("====????", this.areadata.city.value + this.areadata.province.value + this.areadata.region.value);

        }


        console.info(this.companyName);
        console.info(this.textarea);
        console.info(this.remarks);


    }


}




